package com.juyu.miliao.scheduler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;

import com.juyu.miliao.scheduler.util.SpringContextUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


@SpringBootApplication
@EnableCaching
public class SocialSchedulerApplication {

	private final Logger loggerError = LoggerFactory.getLogger("userUpdateIconError");

	public static void main(String[] args) {
		ApplicationContext socialApiApplication = SpringApplication.run(SocialSchedulerApplication.class, args);
		SpringContextUtil.setApplicationContext(socialApiApplication);
	}

	@Bean
	public CacheManager cacheManager(RedisConnectionFactory factory) {
		// 生成一个默认配置，通过config对象即可对缓存进行自定义配置
		RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
		// 设置一个初始化的缓存空间set集合
		Set<String> cacheNames =  new HashSet<>();
		cacheNames.add("timeout-cache-labelCache-scheduler");
		cacheNames.add("timeout-cache-scheduler-getIntegralLevelAndTitle");
		// 对每个缓存空间应用不同的配置
		Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
		configMap.put("timeout-cache-labelCache-scheduler",
				config.entryTtl(Duration.ofMinutes(10)));
		configMap.put("timeout-cache-scheduler-appKeyCache",
				config.entryTtl(Duration.ofMinutes(10)));
		configMap.put("timeout-cache-scheduler-getIntegralLevelAndTitle"
				,config.entryTtl(Duration.ofMinutes(10)));
		RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
				.initialCacheNames(cacheNames)
				.withInitialCacheConfigurations(configMap)
				.build();
		return cacheManager;
	}

	@Bean(destroyMethod = "shutdown")
	public ThreadPoolExecutor updateUserIconThreadPoolExecutor(){
		//线程异常处理机制
		Thread.setDefaultUncaughtExceptionHandler((thread, e) -> {
			loggerError.error(e.getMessage());
		});
		//获取计算机cpu数量
//		int num = Runtime.getRuntime().availableProcessors();
		//自定义定长线程池
		ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 8,
				60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadFactory() {
			private final AtomicInteger counter = new AtomicInteger();
			@Override
			public Thread newThread(Runnable r) {
				Thread thread = new Thread(r);
				thread.setName("update-user-icon-thread-pool-" + counter.incrementAndGet());
				return thread;
			}
		});
		return executor;
	}

}
